/**
 * 
 */
function validateForm() {
	var div = document.getElementById("error");
	while (div.hasChildNodes()) {
		div.removeChild(div.childNodes[0]);
	}
	var foodName = document.forms["food"]["foodName"].value;
	var oldPrice = document.forms["food"]["oldPrice"].value;
	var newPrice = document.forms["food"]["newPrice"].value;
	var textError = null;
	var check = true;
	if (foodName == null || foodName == "") {
		textError = "Food name must be filled out";
		addElements(div, textError);
		check = false;
	}
	if (oldPrice == null || oldPrice == "") {
		textError = "Old price name must be filled out";
		addElements(div, textError);
		check = false;
	} else if (oldPrice < 0) {
		textError = "Old price name must be greater than or equal 0";
		addElements(div, textError);
		check = false;
	}
	if (newPrice < 0) {
		textError = "New price name must be greater than or equal 0";
		addElements(div, textError);
		check = false;
	}
	if (checkExistedFood() == true) {
		textError = "Food name has existed!";
		addElements(div, textError);
		check = false;
	}

	return check;
}

function addElements(div, textError) {
	var dv = document.createElement("DIV");
	dv.setAttribute("class", "alert alert-danger");
	var sp1 = document.createElement("SPAN");
	sp1.setAttribute("class", "glyphicon glyphicon-exclamation-sign");
	dv.appendChild(sp1);
	var text = document.createTextNode(textError);
	dv.appendChild(text);
	div.appendChild(dv);
}

function checkExistedFood() {
	var flag = false;
	$.ajax({
		"type" : "GET",
		"async" : false,
		"url" : "/core-web-admin/food/checkexistedfood",
		"data" : {
			"menuFoodId" : $('#menuFoodId').val(),
			"foodName" : $('#foodName').val()
		},
		"success" : function(data) {
			if (data == "true") {
				flag = true;
			}
			if (data == "false") {
				flag = false;
			}
		},
		"error" : function(data) {
			alert("ERROR WHEN CHECK EXISTED FOOD NAME!");
			flag = false;
		}
	});
	return flag;
}

function deleteFood() {
	var flag = false;
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/core-web-admin/food/delete",
		"data" : {
			"menuFoodId" : $('#menuFoodId').val(),
			"foodId" : $('#foodId').val()
		},
		"success" : function(data) {
			if (data == "true") {
				flag = true;
			}
			if (data == "false") {
				flag = false;
			}
		},
		"complete" : function(data) {
			location.href = "/core-web-admin/food/list";
		},
		"error" : function(data) {
			alert("ERROR WHEN CHECK DELETE FOOD NAME!");
			flag = false;
		}
	});
	return flag;
}

function validateUpdateForm() {
	var div = document.getElementById("error");
	while (div.hasChildNodes()) {
		div.removeChild(div.childNodes[0]);
	}
	var oldPrice = document.forms["updatefood"]["oldPrice"].value;
	var newPrice = document.forms["updatefood"]["newPrice"].value;
	var textError = null;
	var check = true;
	if (oldPrice == null || oldPrice == "") {
		textError = "Old price name must be filled out";
		addElements(div, textError);
		check = false;
	} else if (oldPrice < 0) {
		textError = "Old price name must be greater than or equal 0";
		addElements(div, textError);
		check = false;
	}
	if (newPrice < 0) {
		textError = "New price name must be greater than or equal 0";
		addElements(div, textError);
		check = false;
	}
	return check;
}

